Skip to main content
Version: 1.0.0

weeklyPublishedVariantsReport

Weekly Published Variants Report Template

Function Names: weeklyPublishedVariantsReport / testWeeklyPublishedVariantsReport

Author: Domenico Cerone Creation Date: 02/10/2025
Last Reviewer: Domenico Cerone

Trigger: Scheduler (onSchedule - every Monday at 17:00) / HTTPS (onRequest)

Purpose: Executes a weekly notification (every Monday at 17:00) to monitor SKUs in 'In Pubblicazione' and 'Rev. Cliente' status for each CatalogOrder (aggregating all batches). Sends summary emails only to Validators and Clients associated with specific CatalogOrders, avoiding generic notifications to the entire system.

Detailed Functionality​

This Firebase Function performs the following operations in sequence:

1. MainBrands Data Collection​

  • Retrieves all brands from the MainBrands collection in the database
  • For each brand, processes all associated variants to count status occurrences

2. Variants Grouping and Counting​

  • For each brand, groups Variants by batch field
  • Counts variants with status 'In Pubblicazione' (skuPublication)
  • Counts variants with status 'Rev. Cliente' (skuRevClient)
  • Creates brand/batch combinations for reporting

3. Profile Filtering and Brand Association​

  • Retrieves only profiles with role 'Validator' and 'Cliente' from Profiles collection
  • Filters profiles by brand using the list_main_brands field
  • NEW: Checks blockEmail array to exclude users who have blocked 'weeklyPublishedVariantsReport' notifications
  • Associates each user with their specific brands for targeted notifications

4. NOTIFICATION PREFERENCE CHECK​

  • NEW FEATURE: Before sending any email, the function checks the recipient's profile for notification preferences
  • Check Field: notification_types.weeklyPublishedVariantsReport in the user's profile
  • Logic: If notification_types.weeklyPublishedVariantsReport is false, the email is NOT sent
  • Default Behavior: If the field is missing or true, the email is sent normally
  • Granular Control: Users can enable/disable specific notification types without affecting others
  • Profile Field: notification_types is a map containing boolean values for each notification type
  • Example: notification_types: { weeklyPublishedVariantsReport: false } disables this notification

5. Conditional Email Sending Logic​

Emails are sent ONLY if:

  • There are SKUs to report (skuPublication > 0 OR skuRevClient > 0)
  • There are recipients for that specific brand
  • Brand has associated Validators or Clients in their list_main_brands
  • NEW: Recipients have NOT blocked 'weeklyPublishedVariantsReport' in their blockEmail array

6. Email Configuration and Recipients​

  • Primary Recipients (TO): Validators who have the specific brand in their list_main_brands and have NOT blocked 'weeklyPublishedVariantsReport'
  • Copy Recipients (CC): Clients who have the specific brand in their list_main_brands and have NOT blocked 'weeklyPublishedVariantsReport'
  • Email Service: ZeptoMail with predefined template (MAIL_TEMPLATE_KEY: 13ef.8598f19fbcc5adb.k1.afbab030-45ff-11f0-832a-66e0c45c7bae.1975a0d02b3)
  • Fallback: Nodemailer for error cases

7. Template Data Variables​

The email template receives these merge variables:

  • brandName: Name of the brand
  • batch: Batch identifier
  • skuPublication: Number of SKUs in 'In Pubblicazione' status
  • skuRevClient: Number of SKUs in 'Rev. Cliente' status

8. Email Blocking System​

  • NEW FEATURE: Users can block specific notifications using the blockEmail array in their profile
  • Blocking Mechanism: Users add 'weeklyPublishedVariantsReport' to their blockEmail array to opt-out
  • Automatic Exclusion: Users with blocked notifications are automatically excluded from email sending
  • Granular Control: Users can block individual notification types without affecting other notifications
  • Profile Field: blockEmail is an array of strings containing notification names to block
  • Example: blockEmail: ["weeklyPublishedVariantsReport", "monthlyReport"] blocks both notifications

9. Automatic Scheduling​

  • Schedule: Every Monday at 17:00 CET (0 17 * * 1)
  • Region: europe-central2
  • Timezone: Europe/Rome

10. Testing Function​

  • HTTP Endpoint: testMonthlyPublishedVariantsReport
  • Method: GET (no parameters required)
  • Purpose: Manual testing with detailed JSON response
  • Behavior: Identical logic but returns comprehensive debugging information

Input (Payload):​

For weeklyPublishedVariantsReport (Scheduled):

Automatic execution - no input required

For testMonthlyPublishedVariantsReport (HTTP):

Method: GET
No body parameters required

Output (Success):​

{
"success": true,
"message": "🧪 Report TEST generato con successo",
"dryRunMode": false,
"timestamp": "2024-01-19T17:00:00.000Z",
"statistics": {
"totalCatalogOrders": 5,
"catalogOrdersWithSkuToReport": 3,
"totalProfiles": {
"validators": 3,
"clients": 2,
"blocked": 1
},
"emailResults": {
"sent": 3,
"skipped": 2,
"errors": 0
}
},
"results": [
{
"catalogOrderId": "catalog123",
"orderName": "Spring Collection 2024",
"skuPublication": 15,
"skuRevClient": 3
}
],
"profilesByCatalogOrder": {
"catalog123": {
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
},
"emailResults": [
{
"catalogOrder": "Spring Collection 2024",
"status": "sent",
"skuPublication": 15,
"skuRevClient": 3,
"recipients": {
"to": 1,
"cc": 1,
"validators": ["validator1@example.com"],
"clients": ["client1@example.com"]
}
}
]
}

This output is returned in the HTTP response (Postman, browser, curl)

Testing​

URL (if HTTPS): http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport

Test with Emulator:

  1. Start the Firebase emulator: firebase emulators:start --only functions
  2. Ensure you're using Node.js version 20: nvm use 20
  3. Test with curl:
curl "http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport"

Test with Dry Run Mode:

curl "http://127.0.0.1:5001/arshades-7e18a/europe-central2/testWeeklyPublishedVariantsReport?dryRun=true"

Postman Testing:

Deploy Command:​

firebase deploy --only functions:weeklyPublishedVariantsReport

Production URL:​

weeklyPublishedVariantsReport: https://europe-central2-arshades-7e18a.cloudfunctions.net/weeklyPublishedVariantsReport

Note: This function runs automatically every Monday at 17:00. The URL is for reference only as scheduled functions cannot be called directly.

testWeeklyPublishedVariantsReport: Not deployed to production (test function only)